package com.diancan;

import java.util.ArrayList;
import java.util.List;

import com.SqlLiteDB.MenuDataHelper;
import com.Utils.MenuUtils;
import com.declare.Declare;
import com.mode.CategoryObj;
import com.model.AllDomain;
import com.model.Category;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MenuGroup extends ActivityGroup {

	public LinearLayout rootLayout;
	public LocalActivityManager activityManager;
	AllDomain infos;
	Declare declare;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.menugroup);
		declare=(Declare)getApplicationContext();
		rootLayout=(LinearLayout)findViewById(R.id.group_Layout);
		rootLayout.removeAllViews();
		activityManager = getLocalActivityManager();
		try {
			infos=MenuUtils.DownloadMenusData("");	
			declare.getMenuListDataObj().InitMenuList(infos.getAddRcipes());
			
			if(declare.getMenuListDataObj().getMenuObjs().size()>0)
			{
				if(declare.curDeskObj!=null)
				{
					//同步数据
					declare.SyncMenuList();
				}
				//初始化分类菜单字典表
				for(Category category:infos.getAddCategories())
				{
					CategoryObj categoryObj=new CategoryObj(category);
					declare.getMenuListDataObj().categoryObjs.add(categoryObj);
				}
				declare.getMenuListDataObj().initCategoryMenuList();
//				declare.getMenuListDataObj().InitHashMenuList();
			}
//			declare.setCategoryList(infos.getAddCategories());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast toast = Toast.makeText(MenuGroup.this, "请求失败！", Toast.LENGTH_SHORT); 
			toast.show();
			return;
		}
		Intent intent=new Intent(MenuGroup .this,CategoryList.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        Window subActivity=getLocalActivityManager().startActivity("CategoryList",intent);
        View view=subActivity.getDecorView();
        rootLayout.addView(view);  
        LayoutParams params=(LayoutParams) view.getLayoutParams();
        params.width=LayoutParams.FILL_PARENT;
        params.height=LayoutParams.FILL_PARENT;
        view.setLayoutParams(params);
        
		
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK)
		{
			String strid=activityManager.getCurrentId();
			if(strid==null)
			{
				return super.onKeyDown(keyCode,event);
			}
			else if(strid.equals("MenuFlipPage")||strid.equals("SearchList"))
			{
				Activity activity=activityManager.getCurrentActivity();
				Window w1=activity.getWindow();
				View v1=w1.getDecorView();
				Animation sAnimation=AnimationUtils.loadAnimation(MenuGroup.this, R.anim.close_out);
				v1.startAnimation(sAnimation);
				rootLayout.removeAllViews();
				
				
				Intent intent=new Intent(MenuGroup .this,CategoryList.class);
		        Window subActivity=getLocalActivityManager().startActivity("CategoryList",intent);
		        View view=subActivity.getDecorView();
		        rootLayout.addView(view);  
		        LayoutParams params=(LayoutParams) view.getLayoutParams();
		        params.width=LayoutParams.FILL_PARENT;
		        params.height=LayoutParams.FILL_PARENT;
		        view.setLayoutParams(params);
				return true;
			}
			else {
				return super.onKeyDown(keyCode,event);
			}
		}else
			return super.onKeyDown(keyCode,event);
	}

    @Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
    	return this.getCurrentActivity().onTouchEvent(event);
	}
}
